package com.baidu.ueditor.upload;

import java.io.InputStream;

import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.State;
import com.eunion.core.LogUtil;
import com.eunion.fileserver.upload.FileUploadFactory;
import com.eunion.fileserver.upload.FileVo;
import com.eunion.common.util.CommUtils;

/**
 * 直接调用ftp生成
 *
 */
public class FtpState {

	public static State saveFtpFileByInputStream(String name, InputStream is) {
		State state = new BaseState(true);
		try {
			FileVo fv = new FileVo();
			String end = name.substring(name.lastIndexOf("."), name.length());
			String newName = CommUtils.getRandomUUID32() + end;

			fv.setNameOrg(name);
			fv.setFileInputStream(is);
			fv.setType(end);
			fv.setNameNew(newName);
			FileUploadFactory.uploadFileInputStream(fv);
			String result = fv.getPath() + fv.getNameNew();
			state.putInfo("title", newName);
			state.putInfo("url", "/" + result);
		} catch (Exception e) {
			LogUtil.get(FtpState.class).debug(e.getMessage(), e);
			e.printStackTrace();
		}
		return state;
	}
}
